package com.zt.java.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 代理工厂
 * created by yuqingfu 2020/01/02
 */
public class ProxyFactory implements InvocationHandler{

    private InterceptorChain interceptorChain;

    private Object target;

    public ProxyFactory(Object target,InterceptorChain interceptorChain) {
        this.target = target;
        this.interceptorChain = interceptorChain;
    }

    public Object getProxy(){
       return  Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object intercept = interceptorChain.intercept(new Invocation(target, method, args));
        if(null == intercept){
            return target;
        }
        return intercept ;
    }



}
